#pragma once
#include <vector>
#include "SOCK_Acceptor.h"
#include "Reactor.h"
//#include "EventHandler.h"
#include "EventDemultiplex.h"
#include "EventHandler.h"

namespace Network
{
	typedef struct {
		u_int count;
		bool isActive;
		EventHandler *eventHandler;
		EventType eventType;
	} EventTuple;


	class SelectReactor : public Reactor {
	private:
		static SelectReactor* instance_;
		fd_set readFDs, writeFDs, exceptFDs;
		SelectReactor(EventDemultiplex* _demultiplexer);
		void ConvertFDsToSets(fd_set &readFDs, fd_set &writeFDs, fd_set &exceptFDs);
	protected: 
		EventDemultiplex* demultiplexer;
		std::vector<EventTuple*> eventTuple;
		bool inEventTuple(EventTuple* t);
		SelectReactor(void);
		~SelectReactor(void);

	public:
		static SelectReactor* instance(void);
		
		virtual void register_handler(EventHandler *eh, EventType et);
		virtual void remove_handler(EventHandler *eh, EventType et);
		virtual void suspend_handle(HANDLE h, EventType et);
		virtual void resume_handle(HANDLE h, EventType et);
	
		void handle_events(timeval *timeout = 0);
	};
}